package dao;

import bean.AdminBean;
import bean.UserBean;
import tools.UtilJDBC;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @Author 王志康
 * @Date 2021/9/5 11:26
 * @Version 1.0.0
 */
public class AdminDao {
    /**
     * 获取权限等级
     *
     * @param number 要获取的账号
     * @return 权限等级
     */
    public static int getLevel(String number) {
        Connection connection = UtilJDBC.connection;
        String sql = "select admin_level from admin where admin_number=?";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, number);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                return resultSet.getInt("admin_level");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 1;
    }

    /**
     * 检查此账号或电话是否已被注册
     *
     * @param admin 待注册的管理员
     * @return 此管理员是否已注册
     */
    public static boolean verification(AdminBean admin) {
        Connection connection = UtilJDBC.connection;
        String sql = "select admin_number from admin where admin_number=?";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, admin.getNumber());
            ResultSet resultSet = statement.executeQuery();
            return !resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 管理员注册
     *
     * @param admin 传入新注册的管理员
     */
    public static void register(AdminBean admin) {
        Connection connection = UtilJDBC.connection;
        String sql = "insert into admin " +
                "(admin_number, admin_password, admin_level)" +
                "values(?,?,?)";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, admin.getNumber());
            statement.setString(2, admin.getPassword());
            statement.setInt(3, admin.getLevel());
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /**
     * 管理员登录
     *
     * @param admin 待登录用户
     * @return 此登录的管理员数据
     */
    public static AdminBean login(AdminBean admin) {
        Connection connection = UtilJDBC.connection;
        String sql = "select admin_number, admin_password, admin_level " +
                "from admin where admin_number=? and admin_password=?";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, admin.getNumber());
            statement.setString(2, admin.getPassword());
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                return new AdminBean(
                        resultSet.getString("admin_number"),
                        resultSet.getString("admin_password"),
                        resultSet.getInt("admin_level")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 更新管理员密码或积分
     *
     * @param admin 更改的管理员信息
     */
    public static void updateAdmin(AdminBean admin) {
        Connection connection = UtilJDBC.connection;
        String sql = "update admin set " +
                "admin_password=?,admin_level=? " +
                "where admin_number=?";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, admin.getPassword());
            statement.setInt(2, admin.getLevel());
            statement.setString(3, admin.getNumber());
            System.out.println(sql);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除管理员(管理员专属方法)
     *
     * @param admin 待删除的管理员信息
     */
    public static void deleteAdmin(AdminBean admin) {
        Connection connection = UtilJDBC.connection;
        String sql = "delete from admin where admin_number=? and admin_password=?";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, admin.getNumber());
            statement.setString(2, admin.getPassword());
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取所有管理员信息
     *
     * @return 包含所有管理员信息的arraylist
     */
    public static ArrayList<AdminBean> getAdminList() {
        Connection connection = UtilJDBC.connection;
        ArrayList<AdminBean> list = new ArrayList<>();
        String sql = "select admin_number,admin_password,admin_level " +
                "from admin";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                list.add(new AdminBean(
                        resultSet.getString("admin_number"),
                        resultSet.getString("admin_password"),
                        resultSet.getInt("admin_level")
                ));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
